package com.mmm.common.utils;

import com.alibaba.fastjson.JSON;
import com.mmm.common.vo.Paging;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;

import java.util.ArrayList;
import java.util.List;

/**
 * @email 1067559930@qq.com
 * @author: One
 * @date: 2022/02/26
 * @time: 11:30
 */
public class EsPage {

    public static<T>  Paging<T> simplePage(Class<T> tClass, SearchResponse search, Integer page, Integer size) {
        long totalPage = search.getHits().getTotalHits().value;
        Long total = size < totalPage ? totalPage : size;
        List<T> list = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            String source = hit.getSourceAsString();
            T t = JSON.parseObject(source, tClass);
            list.add(t);
        }
        return new Paging<>(total,totalPage,page,size,list);
    }
    public static<T>  Paging<T> multilevelPage(List<T> tList,SearchResponse search,Integer page,Integer size) {
        long totalPage = search.getHits().getTotalHits().value;
        Long total = size < totalPage ? totalPage : size;
        return new Paging<>(total,totalPage,page,size,tList);
    }
}
